package com.icangya.DAO.impl;

import com.icangya.DAO.IRechargeRecordDAO;
import com.icangya.Utils.SQLUtilt;
import com.icangya.domain.RechargeRecord;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import static com.icangya.Value.FootBall.FOOT_PAGE_ROW;
import static com.icangya.Value.StaticValue.PHONE_PAGE_ROW;

@Repository
public class RechargeRecordDAOImpl  extends BaseDAO<RechargeRecord> implements IRechargeRecordDAO{

    @Override
    public List getRechargeRecord() {
        String hql="FROM RechargeRecord rr order by rr.rechargeTime desc";
       List<RechargeRecord>  list= (List<RechargeRecord>) getHibernateTemplate().find(hql);
       if(list.size()>0)
           return list;
        return null;
    }

    @Override
    public List getRechargeRecordByOpenId(String openId) {
        Session session=  getHibernateTemplate().getSessionFactory().getCurrentSession();
        String hql=" FROM RechargeRecord rr where rr.openId='"+openId+"'order by rr.rechargeTime desc";
        Query query=session.createQuery(hql);
       query.setMaxResults(14);
        List list=query.list();
        if(list !=null)
            return  list;
        return null;
    }

    @Override
    public List getRechargeRecordByOpenId(String openId, int startPage) {
        String hql="FROM RechargeRecord rr where rr.openId='"+openId+"' order by rr.rechargeTime desc";
        String count = "SELECT COUNT(rr) " + hql;
        Long total = (Long) getHibernateTemplate().find(count).get(0);
//
        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, startPage, FOOT_PAGE_ROW);
//        return null;
    }


    //获取单个用户的首次充值具体金额记录
    @Override
    public List getRechargeByOpenIdOrMoney(String openId, double money) {
        String hql="FROM RechargeRecord rr where rr.openId=?  and  rr.money=?";
        List<RechargeRecord>  list= (List<RechargeRecord>) getHibernateTemplate().find(hql,openId,money);
        if(list.size()>0)
            return list;
        return null;
    }

    @Override
    public Double getRechargeMoneyNow(Timestamp time) {
        String hql="select sum(rr.money) from RechargeRecord rr where TO_DAYS(rr.rechargeTime)=TO_DAYS(?) ";
      Double money= (Double) getHibernateTemplate().find(hql,time).get(0);
      if(money==null)
          money=0.0;
        return money;
    }

    /**
     * 获取充值总额
     * @return
     */
    @Override
    public Double getRechargeMoney() {
        String hql="select sum(rr.money) from RechargeRecord rr ";
        Double money= (Double) getHibernateTemplate().find(hql).get(0);
        if(money==null)
            money=0.0;
        return money;
    }


    @Override
    public Double getSumRechargeMoney(String openId) {
        String hql="select sum(rr.money) from RechargeRecord rr where TO_DAYS(rr.rechargeTime)=TO_DAYS(?) and  rr.openId=?";
         Double sumMoney= (Double) getHibernateTemplate().find(hql,new Timestamp(new Date().getTime()),openId).get(0);
        if(sumMoney==null)
            return 0.0;
        return sumMoney;
    }

    @Override
    public List<Object> findByUserRechargeRecord(Long id, Integer pageNum) {

        String hql="FROM RechargeRecord er where er.openId='"+id+"' order by er.rechargeTime desc";
        String count = "SELECT COUNT(er) " + hql;
        Long total = (Long) getHibernateTemplate().find(count).get(0);
        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, pageNum, FOOT_PAGE_ROW);
    }
}
